package com.lianniu.mgr.projectMgr.projectInfo.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.projectMgr.projectInfo.dao.IProjectMapperDao;
import com.lianniu.mgr.projectMgr.projectInfo.dao.IProjectProcessMapperDao;
import com.lianniu.mgr.projectMgr.projectInfo.dao.IProjectStaffMapperDao;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectStaffVo;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectVo;
import com.lianniu.mgr.projectMgr.projectInfo.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProjectServiceImpl implements IProjectService {

	@Autowired
	IProjectMapperDao projectMapper;
	  
	@Autowired
	IProjectStaffMapperDao projectStaffMapper;
	  
	@Autowired
	IProjectProcessMapperDao projectProcessMapper;
  
	/**
	 * @Description:根据项目名称获取项目的分页列表信息
	 * @param project 项目信息
	 * @return BaseResult(List<ProjectVo>) 查询记录列表

	 * @Date:2016年4月13日下午8:36:19
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult getProjectsByProjectNameForPage(ProjectVo project) {
		List<ProjectVo> list = projectMapper.getProjectsByProjectNameForPage(project);
	    Map<String, Object> retmap = new HashMap<String, Object>();
	    retmap.put("total", project.getPaginationInfo().getTotalRecord());
	    retmap.put("curPage", project.getPaginationInfo().getCurPage());
	    retmap.put("rows", list);
	    return BaseResult.success(retmap);
	}
	
	/**
	 * @Description:获取项目列表信息
	 * @param project 项目信息
	 * @return BaseResult(List<ProjectVo>) 查询记录列表

	 * @Date:2016年4月13日下午8:36:19
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult getProjectsByVo(ProjectVo project) {
		return BaseResult.success(projectMapper.getProjectsByVo(project));
	}
  
	/**
	 * @Description:插入项目记录
	 * @param project 项目信息
	 * @return BaseResult 

	 * @Date:2016年4月13日下午8:36:19
	 * @Version 1.0.0
	 */
	@Override
	@Transactional
	public BaseResult addProject(ProjectVo project, List<ProjectStaffVo> list) {
		projectStaffMapper.addProjectStaffs(list);
		return BaseResult.success(projectMapper.addProject(project));
	}
  
	/**
	 * @Description:根据id更新项目信息
	 * @param project 项目信息
	 * @return BaseResult

	 * @Date:2016年3月29日下午8:36:19
	 * @Version 1.0.0
	 */
	@Override
	@Transactional
	public BaseResult editProjectById(ProjectVo project, List<ProjectStaffVo> list) {
		/*BaseResult baseResult;
		if (StringUtil.isEmpty(project.getId())) {
      	baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      	return baseResult;
    	}
		 */
		ProjectStaffVo projectStaffVo = new ProjectStaffVo();
		projectStaffVo.setProjectId(project.getId());
		// 根据  PostName like "%负责人%" 区分 项目负责人还是其他人员
		projectStaffVo.setPostName("负责人");
		projectStaffMapper.delProjectStaffsByVo(projectStaffVo);
		projectStaffMapper.addProjectStaffs(list);
		return BaseResult.success(projectMapper.editProjectById(project));
	}
	
	/**
	 * @Description:批量删除项目记录
	 * @param ids 项目id组
	 * @return BaseResult

	 * @Date:2016年4月13日下午8:36:19
	 * @Version 1.0.0
	 */
	@Override
	@Transactional
	public BaseResult delProjectsByIds(List<String> ids) {
		projectStaffMapper.delProjectStaffsByProjectIds(ids);
		projectProcessMapper.delProjectProcessesByProjectIds(ids);
		return BaseResult.success(projectMapper.delProjectsByIds(ids));
	}
  
}
